﻿using HookupAppForm.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormTakeOff : Form
    {
        private readonly string url = "api/services/app/HKP_Takeoff/";
        private HKP_TakeoffDto Item { get; set; }
        private APIClient Client { get; set; }
        private long BlockId { get; set; }
        private decimal XAbs { get; set; }
        private decimal YAbs { get; set; }
        public FormTakeOff(HKP_TakeoffDto item, APIClient _client, long blockId = 0, decimal x = 0, decimal y = 0)
        {
            InitializeComponent();
            Item = item;
            Client = _client;
            BlockId = blockId;
            XAbs = x;
            YAbs = y;
            this.Text += "X：" + x.ToString() + " Y：" + y.ToString();
        }

        public ItemsDto<HKP_TakeoffDto> GetTakeoffAll()
        {
            string url = string.Format(this.url + "GetList?maxResultCount=99999");
            //string url = string.Format("http://localhost:21029/api/services/app/HKP_Takeoff/GetListNew?maxResultCount=9999");
            var result = Client.Get(url);
            ItemsDto<HKP_TakeoffDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_TakeoffDto>>(result);
            return res;
        }
        private void LoadData()
        {

            string SubmainId = this.combSelectGD.SelectedValue.ToString();
            string url = string.Format(this.url + "GetListNew?maxResultCount=9999");
            //string url = string.Format("http://localhost:21029/api/services/app/HKP_Takeoff/GetListNew?maxResultCount=9999");
            if (!string.IsNullOrEmpty(SubmainId))
            {
                url = string.Format(url + "&sysid=" + SubmainId + "");
            }
            var result = Client.Get(url);
            ItemsDto<HKP_TakeoffDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_TakeoffDto>>(result);
            dataGridViewM.DataSource = res?.result?.items;
            FromSetting[] fromSettings = new FromSetting[50];
            fromSettings[43] = new FromSetting { FieldDisplay = "系统",FieldDisplayIndex=1 };
            fromSettings[1] = new FromSetting { FieldDisplay = "接点编号", FieldDisplayIndex = 2 };
            fromSettings[2] = new FromSetting { FieldDisplay = "接点名称", FieldDisplayIndex = 3 };
            fromSettings[4] = new FromSetting { FieldDisplay = "接点尺寸/电压", FieldDisplayIndex = 4 };
            fromSettings[38] = new FromSetting { FieldDisplay = "尺寸单位/电源种类", FieldDisplayIndex = 5 };
            fromSettings[7] = new FromSetting { FieldDisplay = "设计(流量/容量)", FieldWidth = 120, FieldDisplayIndex = 6 };
            fromSettings[11] = new FromSetting { FieldDisplay = "（流量/容量）单位", FieldWidth = 120, FieldDisplayIndex = 7 };
            fromSettings[5] = new FromSetting { FieldDisplay = "图形类型", FieldDisplayIndex = 8 };
            fromSettings[35] = new FromSetting { FieldDisplay = "备注", FieldWidth = 120, FieldDisplayIndex = 9 };
            fromSettings[33] = new FromSetting { FieldDisplay = "启用", FieldDisplayIndex = 10 };
            fromSettings[34] = new FromSetting { FieldDisplay = "占用", FieldDisplayIndex = 11 };



            //fromSettings[6] = new FromSetting { FieldDisplay = "接点位置", FieldDisplayIndex = 6 };
            Utils.SetHeader(dataGridViewM, fromSettings);
        }

        private void FormTakeOff_Load(object sender, EventArgs e)
        {
            string url = string.Format("api/services/app/HKP_System/GetList");
            var result = Client.Get(url);
            ItemsDto<HKP_SystemDto> dto = JsonConvert.DeserializeObject<ItemsDto<HKP_SystemDto>>(result);
            combSelectGD.DataSource = dto?.result?.items?.ToList();
            combSelectGD.DisplayMember = "SysNo";
            combSelectGD.ValueMember = "Id";

            //LoadData();
            BindCombSubm();
            BindCombBlock();
            ucDrpPosition.Init("Position", Client.RemoteHost);
            ucDrpOutType.Init("BaseType", Client.RemoteHost);
            ucDrpPressureUnit.Init("Unit", Client.RemoteHost, 0, "Pa");
            ucDrpPowerUnit.Init("Unit", Client.RemoteHost, 0, "kw");
            ucDrpFlowUnit.Init("流量", Client.RemoteHost, 0, "CMH");
            ucDrpOutSize.Init("尺寸", Client.RemoteHost);
            ucDrpOutSizeUnit.Init("尺寸单位", Client.RemoteHost, 0, "in");
            SetItem(Item);
            groupBoxChk.Visible = false;
            groupBoxPress.Visible = false;
            groupBoxPower.Visible = false;
        }
        private void SetItem(HKP_TakeoffDto Item)
        {
            if (Item != null)
            {
                txtTakeoffName.Text = Item.TakeoffName;
                txtTakeiffNo.Text = Item.TakeoffNo;
                combBlock.SelectedValue = Item.BlockId;
                cBoxEnabled.Checked = Item.Enabled;
                numFlowPeak.Value = Item.FlowAvg;
                numFlowMin.Value = Item.FlowMin;
                numFlowPeak.Value = Item.FlowPeak;
                numFlowPeakOpr.Value = Item.FlowPeakOperation;
                ucDrpFlowUnit.SetText(Item.FlowUnit);
                cBoxNeedPreGauge.Checked = Item.NeedPreGauge;
                cBoxNeedTap.Checked = Item.NeedTap;
                cboxNeedValve.Checked = Item.NeedValve;
                ucDrpPosition.SetText(Item.Position);
                numPressureAvg.Value = Item.PressureAvg;
                numPressureMin.Value = Item.PressureMin;
                numPressurePeak.Value = Item.PressurePeak;
                numPressurePeakOper.Value = Item.PressurePeakOper;
                ucDrpPressureUnit.SetText(Item.PressureUnit);
                numPower.Value = Item.Power;
                numPowerOper.Value = Item.PowerOper;
                numPowerAvg.Value = Item.PowerAvg;
                numPowerMin.Value = Item.PowerMin;
                ucDrpPowerUnit.SetText(Item.PowerUnit);
                txtRemark.Text = Item.Remark;
                combSubmain.SelectedValue = Item.SubmainId;
                ucDrpOutType.SetText(Item.OutType);
                ucDrpOutSize.SetText(Item.OutSize);
                ucDrpOutSizeUnit.SetText(Item.OutSizeUnit);
                cBoxNeedBellow.Checked = Item.NeedBellow;
                cBoxNeedFilter.Checked = Item.NeedFilter;
                cBoxNeedFlowmeter.Checked = Item.NeedFlowmeter;
                cBoxNeedTrap.Checked = Item.NeedTrap;
                numVoltage.Value = Item.Voltage;
                numTemperatureWRTMin.Value = Item.TemperatureWRTMin;
                numTemperatureWRTMax.Value = Item.TemperatureWRTMax;
                numX.Value = Item.X;
                numY.Value = Item.Y;
                textBoxUsedFlow.Text = Item.UsedFlow.ToString("0.00");
            }
        }
        private void BindCombSubm()
        {
            string act = string.Format("api/services/app/HKP_Submain/GetList?maxResultCount={0}", 99999);
            var result = Client.Get(act);
            ItemsDto<HKP_SubmainDto> dto = JsonConvert.DeserializeObject<ItemsDto<HKP_SubmainDto>>(result);
            combSubmain.DataSource = dto?.result?.items;
            combSubmain.DisplayMember = "SubmainName";
            combSubmain.ValueMember = "Id";
            combSubmain.SelectedIndex = -1;


        }
        private void BindCombBlock()
        {
            string act = string.Format("api/services/app/HKP_Block/GetList?maxResultCount={0}", 99999);
            var result = Client.Get(act);
            ItemsDto<HKP_BlockDto> dto = JsonConvert.DeserializeObject<ItemsDto<HKP_BlockDto>>(result);
            combBlock.DataSource = dto?.result?.items;
            combBlock.DisplayMember = "BuildAndFloor";
            combBlock.ValueMember = "Id";
            if (BlockId > 0)    
            {
                combBlock.SelectedValue = BlockId;
                combBlock.Enabled = false;
            }
        }
        private HKP_SubmainDto GetSubm(string id)
        {
            string act = string.Format("api/services/app/HKP_Submain/Get?id={0}", id);
            var jsons = Client.Get(act);
            var resp = Utils.SuccessHandler(jsons);
            return JsonConvert.DeserializeObject<HKP_SubmainDto>(resp.ToString());
        }
        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            if (combBlock.SelectedValue == null)
            {
                Utils.SysMsg("请选择区块！", MessageBoxIcon.Warning);
                return;
            }
            if (combSubmain.SelectedValue == null)
            {
                Utils.SysMsg("请选择管道！", MessageBoxIcon.Warning);
                return;
            }
            Guid submId = Guid.Parse(combSubmain.SelectedValue?.ToString());
            var subMain = GetSubm(submId.ToString());
            if (subMain == null)
            {
                Utils.SysMsg("管道系统不存在！", MessageBoxIcon.Warning);
                return;
            }
            var olist = dataGridViewM.DataSource as HKP_TakeoffDto[];
            if (olist != null)
            {
                Guid selfId = Item != null ? Item.Id : new Guid();
                decimal PressurePeakSum = olist.ToList().Where(o => o.SubmainId == submId && o.Id != selfId).Sum(o => o.PressurePeakOper);
                decimal FlowPeakSum = olist.ToList().Where(o => o.SubmainId == submId && o.Id != selfId).Sum(o => o.FlowPeakOperation);
                decimal PowerSum = olist.ToList().Where(o => o.SubmainId == submId && o.Id != selfId).Sum(o => o.PowerOper);
                //if (Utils.ConvertPress(PressurePeakSum + numPressurePeakOper.Value, ucDrpPressureUnit.selectedText) > Utils.ConvertPress(subMain.PressurePeakOper, subMain.PressureUnit))
                //{
                //    Utils.SysMsg("不能超出操作压力负荷" + subMain.PressurePeak + subMain.PressureUnit, MessageBoxIcon.Error);
                //    return;
                //}
                //if (Utils.ConvertFlow(FlowPeakSum + numFlowPeakOpr.Value, ucDrpFlowUnit.selectedText) > Utils.ConvertFlow(subMain.FlowPeakOperation, subMain.FlowUnit))
                //{
                //    Utils.SysMsg("不能超出操作流量负荷" + subMain.FlowPeakOperation + subMain.FlowUnit, MessageBoxIcon.Error);
                //    return;
                //}
                //if (Utils.ConvertWatt(PowerSum + numPowerOper.Value, ucDrpPowerUnit.selectedText) > Utils.ConvertWatt(subMain.PowerOper, subMain.PowerUnit))
                //{
                //    Utils.SysMsg("不能超出操作功率负荷" + subMain.PowerOper + subMain.PowerUnit, MessageBoxIcon.Error);
                //    return;
                //}
            }

            string act = this.url + (Item != null ? "Update" : "Create");
            string result = string.Empty;
            string jsons = string.Empty;
            if (Item != null)
            {
                Item.TakeoffName = txtTakeoffName.Text;
                Item.TakeoffNo = txtTakeiffNo.Text;
                Item.BlockId = Convert.ToInt32(combBlock.SelectedValue);
                Item.Enabled = cBoxEnabled.Checked;
                Item.FlowAvg = numFlowPeak.Value;
                Item.FlowMin = numFlowMin.Value;
                Item.FlowPeak = numFlowPeak.Value;
                Item.FlowPeakOperation = numFlowPeakOpr.Value;
                Item.FlowUnit = ucDrpFlowUnit.selectedText;
                Item.NeedPreGauge = cBoxNeedPreGauge.Checked;
                Item.NeedTap = cBoxNeedTap.Checked;
                Item.NeedValve = cboxNeedValve.Checked;
                Item.OutSize = ucDrpOutSize.selectedText;
                Item.OutSizeUnit = ucDrpOutSizeUnit.selectedText;
                Item.OutType = ucDrpOutType.selectedText;
                Item.Position = ucDrpPosition.selectedText;
                Item.PressureAvg = numPressureAvg.Value;
                Item.PressureMin = numPressureMin.Value;
                Item.PressurePeak = numPressurePeak.Value;
                Item.PressurePeakOper = numPressurePeakOper.Value;
                Item.PressureUnit = ucDrpPressureUnit.selectedText;
                Item.Remark = label29.Text;
                Item.SubmainId = submId;
                Item.Power = numPower.Value;
                Item.PowerOper = numPowerOper.Value;
                Item.PowerAvg = numPowerAvg.Value;
                Item.PowerMin = numPowerMin.Value;
                Item.PowerUnit = ucDrpPowerUnit.selectedText;
                Item.NeedBellow = cBoxNeedBellow.Checked;
                Item.NeedFilter = cBoxNeedFilter.Checked;
                Item.NeedFlowmeter = cBoxNeedFlowmeter.Checked;
                Item.NeedTrap = cBoxNeedTrap.Checked;
                Item.Voltage = numVoltage.Value <= 0 ? 220 : numVoltage.Value;
                Item.ElcCurrent = Item.Power / Item.Voltage;               
                Item.TemperatureWRTMin = numTemperatureWRTMin.Value;
                Item.TemperatureWRTMax = numTemperatureWRTMax.Value;
                Item.X = numX.Value;
                Item.Y = numY.Value;

                jsons = JsonConvert.SerializeObject(Item);
                result = Client.Put(act, jsons);
            }
            else
            {
                CreateHKP_TakeoffDto dto = new CreateHKP_TakeoffDto();
                dto.TakeoffName = txtTakeoffName.Text;
                dto.TakeoffNo = txtTakeiffNo.Text;
                dto.BlockId = Convert.ToInt32(combBlock.SelectedValue);
                dto.Enabled = cBoxEnabled.Checked;
                dto.FlowAvg = numFlowPeak.Value;
                dto.FlowMin = numFlowMin.Value;
                dto.FlowPeak = numFlowPeak.Value;
                dto.FlowPeakOperation = numFlowPeakOpr.Value;
                dto.FlowUnit = ucDrpFlowUnit.selectedText;
                dto.NeedPreGauge = cBoxNeedPreGauge.Checked;
                dto.NeedTap = cBoxNeedTap.Checked;
                dto.NeedValve = cboxNeedValve.Checked;
                dto.OutSize = ucDrpOutSize.selectedText;
                dto.OutSizeUnit = ucDrpOutSizeUnit.selectedText;
                dto.OutType = ucDrpOutType.selectedText;
                dto.Position = ucDrpPosition.selectedText;
                dto.PressureAvg = numPressureAvg.Value;
                dto.PressureMin = numPressureMin.Value;
                dto.PressurePeak = numPressurePeak.Value;
                dto.PressurePeakOper = numPressurePeakOper.Value;
                dto.PressureUnit = ucDrpPressureUnit.selectedText;
                dto.Remark = label29.Text;
                dto.SubmainId = submId;
                dto.Power = numPower.Value;
                dto.PowerOper = numPowerOper.Value;
                dto.PowerAvg = numPowerAvg.Value;
                dto.PowerMin = numPowerMin.Value;
                dto.PowerUnit = ucDrpPowerUnit.selectedText;
                dto.NeedBellow = cBoxNeedBellow.Checked;
                dto.NeedFilter = cBoxNeedFilter.Checked;
                dto.NeedFlowmeter = cBoxNeedFlowmeter.Checked;
                dto.NeedTrap = cBoxNeedTrap.Checked;
                dto.Voltage = numVoltage.Value <= 0 ? 220 : numVoltage.Value;
                dto.ElcCurrent = dto.Power / dto.Voltage;
                dto.TemperatureWRTMin = numTemperatureWRTMin.Value;
                dto.TemperatureWRTMax = numTemperatureWRTMax.Value;
                dto.X = numX.Value;
                dto.Y = numY.Value;
                dto.XAbs = XAbs;
                dto.YAbs = YAbs;
                dto.PointType = "0";

                jsons = JsonConvert.SerializeObject(dto);
                result = Client.Post(act, jsons);
            }

            if (Utils.ExceptionHandler(result) == null)
            {
                Utils.SysMsg("保存成功！", MessageBoxIcon.Information);
                var resp = Utils.SuccessHandler(result);
                Item = JsonConvert.DeserializeObject<HKP_TakeoffDto>(resp.ToString());
                LoadData();
            }
        }

        private void CopyToolStripButton_Click(object sender, EventArgs e)
        {
            if (Item != null)
            {
                string act = this.url + "Create";
                Item.TakeoffNo += "Copy";
                Item.TakeoffName += "Copy";
                Item.Used = false;
                string jsons = JsonConvert.SerializeObject(Item);
                string result = Client.Post(act, jsons);
                if (Utils.ExceptionHandler(result) == null)
                {
                    Utils.SysMsg("复制成功！");
                }
                LoadData();
            }
        }

        private void DelToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除？", "系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (Item != null)
                {
                    string act = string.Format(this.url + "Delete?Id={0}", Item.Id);
                    string result = Client.Delete(act);
                    if (Utils.ExceptionHandler(result) == null)
                    {
                        NewToolStripButton_Click(sender, e);
                        LoadData();
                    }
                }
            }
        }

        private void DataGridViewM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Item = dataGridViewM.CurrentRow.DataBoundItem as HKP_TakeoffDto;
            SetItem(Item);
        }

        private void NewToolStripButton_Click(object sender, EventArgs e)
        {
            ucDrpOutSize.SetText("V");
            ucDrpOutSizeUnit.SetText("N");
            ucDrpFlowUnit.SetText("A");
            txtTakeoffName.Text = "";
            txtTakeiffNo.Text = "";
            label29.Text = "";
            Item = null;
            combBlock.Enabled = true;
        }

        internal void SetLoc(int mouseMoveX, int mouseMoveY)
        {
            numX.Value = mouseMoveX;
            numY.Value = mouseMoveY;
        }

        private async void ImportToolStripButton_Click(object sender, EventArgs e)
        {
            string filePath = Utils.GetFileUrl();
            if (filePath == null || string.IsNullOrEmpty(filePath))
            {
                Utils.SysMsg("请选择文件！",MessageBoxIcon.Warning);
                return;
            }
            DataTable dt = Utils.ExcelToTable(filePath);
            string act = this.url + "Create";
            //var datasource = dataGridViewM.DataSource as HKP_TakeoffDto[];
            HKP_TakeoffDto[] datasource = GetTakeoffAll().result?.items;

            Guid submId = Guid.Parse(combSubmain.SelectedValue?.ToString());
            if (GetSubm(submId.ToString()) == null)
            {
                Utils.SysMsg("管道系统不存在！", MessageBoxIcon.Warning);
                return;
            }
            var blockItems = combBlock.DataSource as HKP_BlockDto[];
            importToolStripButton.Enabled = false;
            await Task.Run(() =>
            {
                foreach (DataRow row in dt.Rows)
                {
                    var block = blockItems.FirstOrDefault(o => o.BlockName == row["区域"].ToString());

                    var str = row[0].ToString();
                    CreateHKP_TakeoffDto dto = new CreateHKP_TakeoffDto();
                    dto.TakeoffName = row["接点名称"].ToString();
                    dto.TakeoffNo = row["接点编号"].ToString();
                    dto.BlockId = block != null ? block.Id : 0;
                    dto.Enabled = true;
                    dto.FlowAvg = Utils.CvtDec(row["平均流量"].ToString());
                    dto.FlowMin = Utils.CvtDec(row["最小流量"].ToString());
                    dto.FlowPeak = Utils.CvtDec(row["设计流量"].ToString());
                    dto.FlowPeakOperation = Utils.CvtDec(row["操作流量"].ToString());
                    dto.FlowUnit = row["流量单位"].ToString();
                    dto.NeedPreGauge = false;
                    dto.NeedTap = false;
                    dto.NeedValve = false;
                    dto.OutSize = row["接点尺寸"]?.ToString();
                    dto.OutSizeUnit = row["尺寸单位"]?.ToString();
                    dto.OutType = row["接点形式"]?.ToString();
                    dto.Position = row["接点位置"]?.ToString();
                    dto.PressureAvg = Utils.CvtDec(row["平均压力"].ToString());
                    dto.PressureMin = Utils.CvtDec(row["最小压力"].ToString());
                    dto.PressurePeak = Utils.CvtDec(row["设计压力"].ToString());
                    dto.PressurePeakOper = Utils.CvtDec(row["操作压力"].ToString());
                    dto.PressureUnit = row["压力单位"].ToString();
                    dto.SubmainId = submId;
                    dto.Power = Utils.CvtDec(row["设计功率"].ToString());
                    dto.PowerOper = Utils.CvtDec(row["操作功率"].ToString());
                    dto.PowerAvg = Utils.CvtDec(row["平均功率"].ToString());
                    dto.PowerMin = Utils.CvtDec(row["最小功率"].ToString());
                    dto.PowerUnit = row["功率单位"].ToString();
                    dto.NeedBellow = false;
                    dto.NeedFilter = false;
                    dto.NeedFlowmeter = false;
                    dto.NeedTrap = false;
                    dto.Voltage = Utils.CvtDec(row["电压"].ToString());
                    dto.ElcCurrent = dto.Power / (dto.Voltage == 0 ? 1 : dto.Voltage);
                    dto.TemperatureWRTMin = Utils.CvtDec(row["最低回水温度"].ToString());
                    dto.TemperatureWRTMax = Utils.CvtDec(row["最高回水温度"].ToString());
                    dto.X = Utils.CvtDec(row["X"].ToString());
                    dto.Y = Utils.CvtDec(row["Y"].ToString());
                    dto.PointType = "0";

                    int num = datasource.Count(o => o.TakeoffNo == dto.TakeoffNo);
                    if (num <= 0)
                    {
                        string jsons = JsonConvert.SerializeObject(dto);
                        string result = Client.Post(act, jsons);
                        var resp = Utils.ExceptionMsg(result);
                        if (resp != null)
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                this.StatusLabelMsg.Text = dto.TakeoffNo + "导入发生错误：" + resp.error?.message;
                            }));
                        }
                        else
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                this.StatusLabelMsg.Text = dto.TakeoffNo + "导入成功";
                            }));
                        }
                    }
                    else
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            this.StatusLabelMsg.Text = dto.TakeoffNo + "已存在";
                        }));
                    }
                }
            });
            
            Utils.SysMsg("导入完成！");
            importToolStripButton.Enabled = true;
            LoadData();
        }

        private void buttonPos_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTakeiffNo.Text))
            {
                var datasource = dataGridViewM.DataSource as HKP_TakeoffDto[];
                var obj = datasource.FirstOrDefault(o => o.TakeoffNo == txtTakeiffNo.Text);
                if (obj == null)
                {
                    Utils.SysMsg("无此记录！");
                    return;
                }
                Item = obj;
                SetItem(obj);
            }
        }
        //查询按钮
        private void button1_Click(object sender, EventArgs e)
        {
           // double x = Utils.ToInA("12", "KW");

            string seletName = this.txtSelectName.Text.Trim();
            string url = string.Format(this.url + "GetListNew?maxResultCount=9999");
            if (!string.IsNullOrEmpty(seletName))
            {
                url = string.Format(url + "&takeoffno=" + seletName + "");
            }
            else
            {
                MessageBox.Show("请输入接点编号或者接点名称！");
            }
            var result = Client.Get(url);
            ItemsDto<HKP_TakeoffDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_TakeoffDto>>(result);
            dataGridViewM.DataSource = res?.result?.items;
            FromSetting[] fromSettings = new FromSetting[50];
            fromSettings[43] = new FromSetting { FieldDisplay = "系统", FieldDisplayIndex = 1 };
            fromSettings[1] = new FromSetting { FieldDisplay = "接点编号", FieldDisplayIndex = 2 };
            fromSettings[2] = new FromSetting { FieldDisplay = "接点名称", FieldDisplayIndex = 3 };
            fromSettings[4] = new FromSetting { FieldDisplay = "接点尺寸/电压", FieldDisplayIndex = 4 };
            fromSettings[38] = new FromSetting { FieldDisplay = "尺寸单位/电源种类", FieldDisplayIndex = 5 };
            fromSettings[7] = new FromSetting { FieldDisplay = "设计(流量/容量)", FieldWidth = 120, FieldDisplayIndex = 6 };
            fromSettings[11] = new FromSetting { FieldDisplay = "（流量/容量）单位", FieldWidth = 120, FieldDisplayIndex = 7 };
            fromSettings[5] = new FromSetting { FieldDisplay = "图形类型", FieldDisplayIndex = 8 };
            fromSettings[35] = new FromSetting { FieldDisplay = "备注", FieldWidth = 120, FieldDisplayIndex = 9 };
            fromSettings[33] = new FromSetting { FieldDisplay = "启用", FieldDisplayIndex = 10 };
            fromSettings[34] = new FromSetting { FieldDisplay = "占用", FieldDisplayIndex = 11 };

            Utils.SetHeader(dataGridViewM, fromSettings);
        }

        private HKP_SystemDto GetSystem(string id)
        {
            string act = string.Format("api/services/app/HKP_System/get?id={0}", id);
            var jsons = Client.Get(act);
            var resp = Utils.SuccessHandler(jsons);
            return JsonConvert.DeserializeObject<HKP_SystemDto>(resp.ToString());
        }

      
        private void combSubmain_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string x = this.combSubmain.SelectedValue.ToString();
            
         
            var sunmin = GetSubm(x);

            var sysname = GetSystem(sunmin.SystemId.ToString());

            if (sysname.SysNo == "ES")
            {
                ucDrpFlowUnit.Init("容量", Client.RemoteHost, 0, "A");
                ucDrpOutSize.Init("电压", Client.RemoteHost);
                ucDrpOutSizeUnit.Init("电源种类", Client.RemoteHost, 0, "N");
               
            }
            else
            {
                ucDrpFlowUnit.Init("流量", Client.RemoteHost, 0, "CMH");
                ucDrpOutSize.Init("尺寸", Client.RemoteHost);
                ucDrpOutSizeUnit.Init("尺寸单位", Client.RemoteHost, 0, "in");
            }
        }


        //系统下拉搜索
        private void combSelectGD_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string SubmainId = this.combSelectGD.SelectedValue.ToString();
            string url = string.Format(this.url + "GetListNew?maxResultCount=9999");
            //string url = string.Format("http://localhost:21029/api/services/app/HKP_Takeoff/GetListNew?maxResultCount=9999");
            if (!string.IsNullOrEmpty(SubmainId))
            {
                url = string.Format(url + "&sysid=" + SubmainId + "");
            }
            var result = Client.Get(url);
            ItemsDto<HKP_TakeoffDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_TakeoffDto>>(result);
            dataGridViewM.DataSource = res?.result?.items;
            FromSetting[] fromSettings = new FromSetting[50];
            fromSettings[43] = new FromSetting { FieldDisplay = "系统", FieldDisplayIndex = 1 };
            fromSettings[1] = new FromSetting { FieldDisplay = "接点编号", FieldDisplayIndex = 2 };
            fromSettings[2] = new FromSetting { FieldDisplay = "接点名称", FieldDisplayIndex = 3 };
            fromSettings[4] = new FromSetting { FieldDisplay = "接点尺寸/电压", FieldDisplayIndex = 4 };
            fromSettings[38] = new FromSetting { FieldDisplay = "尺寸单位/电源种类", FieldDisplayIndex = 5 };
            fromSettings[7] = new FromSetting { FieldDisplay = "设计(流量/容量)", FieldWidth = 120, FieldDisplayIndex = 6 };
            fromSettings[11] = new FromSetting { FieldDisplay = "（流量/容量）单位", FieldWidth = 120, FieldDisplayIndex = 7 };
            fromSettings[5] = new FromSetting { FieldDisplay = "图形类型", FieldDisplayIndex = 8 };
            fromSettings[35] = new FromSetting { FieldDisplay = "备注", FieldWidth = 120, FieldDisplayIndex = 9 };
            fromSettings[33] = new FromSetting { FieldDisplay = "启用", FieldDisplayIndex = 10 };
            fromSettings[34] = new FromSetting { FieldDisplay = "占用", FieldDisplayIndex = 11 };

            Utils.SetHeader(dataGridViewM, fromSettings);
        }
    }
}
